package com.qf.mvc2203.ex;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
import java.util.List;

/**
 * 统一异常处理器
 * 注意：要把 自定义的统一异常处理器放入IOC容器中才可以生效
 */
public class MyExResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
          e.printStackTrace(); //把异常的所有信息打印到控制台上
      //因为这个类的返回值是ModelAndView，所以创建一个他的对象（固定写法，不会就cv）
        final ModelAndView mv = new ModelAndView();
       //返回页面 视图
        if (e instanceof UserException){
            mv.setViewName("redirect:/error01.html");

        }else {

            mv.setViewName("redirect:/error02.html");
        }

        return mv;
    }
}
